// pages/acknowledgementOrder/index.js
const app = getApp();
const $api = require('../../api/index');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        data: {},
        payType: 0,
        ticket_num: 0,
        couponbox: -1,
        show: false,
        trade_no: "",
        loading:false
    },
    topUp() {
        wx.navigateTo({
            url: `../topUp/index`,
        })
    },
    onChange(event) {
        this.setData({
            payType: event.detail,
        });
    },
    onYouhunquanShow: function () {
        this.setData({
            show: true
        });
    },
    onClose: function () {
        this.setData({
            show: false
        });
    },
    changeYouhuiqun(event) {
        const {
            couponbox
        } = event.currentTarget.dataset;
        if (this.data.couponbox == couponbox) {
            this.setData({
                couponbox: -1
            });
            return;
        }
        this.setData({
            couponbox,
            show: false
        });
    },
    onClick(event) {
        const {
            pay_type_id
        } = event.currentTarget.dataset;
        this.setData({
            payType: pay_type_id,
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData(options)
        this.getPlaceOrderDetail(options);
    },
    getPlaceOrderDetail(e) {
        $api.placeOrderDetail(e).then(res => {
            if (res.result == 1) {
                this.setData({
                    data: res.data,
                    payType: res.data.payTypeList[0].pay_type_id,
                    couponbox:res.data.ticketList&&res.data.ticketList.length>0?res.data.ticketList.findIndex((item,index)=>item.i_check):-1
                })
            };
        })
    },
    // 支付订单
    postPlaceOrderDetail() {
        this.setData({
            loading:true
        })
        $api.selectPaytype({
            trade_no: this.data.trade_no,
            pay_type_id: this.data.payType,
            ticket_num: this.data.couponbox == -1 ? "" : this.data.data.ticketList[this.data.couponbox].ticket_num
        }).then(res => {
            this.setData({
                loading:false
            })
            if (res.result == 1) {
                if (this.data.payType == 1567) {
                    this.callPayment(res.data);
                } else {
                    this.callUpPayment(res.data);
                }
            };
        })
    },
    // 微信电子卡
    callPayment(e) {
        const _this = this;
        $api.orderDoPay({
            sub_trade_no: e
        }).then(res => {
            if (res.result == 1) {
                wx.navigateTo({
                    url: `../topPaySucc/index?trade_no=${e}&payType=${_this.data.payType}`
                })
            };
        })
    },
    // 微信支付
    callUpPayment(e) {
        const _this = this;
        $api.orderDoPay({
            sub_trade_no: e
        }).then(res => {
            if (res.result == 1) {
                wx.requestPayment({
                    timeStamp: res.data.timeStamp,
                    nonceStr: res.data.nonceStr,
                    package: res.data.packageValue,
                    signType: res.data.signType,
                    paySign: res.data.paySign,
                    success() {
                        wx.navigateTo({
                            url: `../topPaySucc/index?trade_no=${e}&payType=${_this.data.payType}`
                        })
                    },
                    fail(res) {
                        wx.showToast({
                            title: res,
                            icon: "error",
                            duration: 3000
                        })
                    }
                });
            };
        })
    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})